/**
 * Created by huangzy on 2015/5/22.
 * http://wx.12301.cc/tpl/membercard/js/membercard.renzheng.js
 */
var ImgUploador = RichBase.extend({
	statics : {
		ltie9 : false
	},
	EVENTS : {
		"change" : {
			"input.fileInp" : "onFileInpChange"
		}
	},
	init : function(opt){
		var that = this;
		this.statics.ltie9 = $("#memCertifyContainer").hasClass("lt-ie9") ? true : false;
		var container = this.container = opt.container;
		var containerId = container.attr("id");
		var form = this.form = container.find("form");
		var label = this.label = container.find("label");
		var fileInp = this.fileInp = container.find("input[type=file]");
		var loading = this.loading = container.find(".loading");
		this.submitBtn = container.find(".ie8formsubmitBtn");
		if(this.statics.ltie9){ //ie8及以下做fix
			label.on("click",function(e){
				fileInp.click();
			})
			container.hover(function(){
				label.css({"backgroundColor":"#000000", opacity:0.8})
			},function(){
				label.css({"backgroundColor":"transparent", opacity:0})
			})
			that.fileInp[0].onchange = function(e){
				that.onFileInpChange(that,e)
			}
		}

	},
	onFileInpChange : function(that,e){
		
		that.onload();
		that.form.submit();
	},
	oncomplete : function(res){
		if(res.status=="ok" && res.key){
			this.onsuccess(res);
		}else{
			this.onfail(res);
		}
		this.loading.hide();
	},
	onload : function(){
		this.loading.show();
	},
	onsuccess : function(res){
		this.container.css("backgroundImage","url("+res.key+")").children(".imgurl").val(res.key);
		PFT_GLOBAL.U.Alert("success",'<p style="width:160px;">上传图片成功</p>');
	},
	onfail : function(res){
		PFT_GLOBAL.U.Alert("fail",'<p style="width:220px;">上传图片失败,请稍后重试</p>');
	}
});
/*
var FormSubmit = RichBase.extend({
	statics : {
		ltie9 : false,
		api : "/r/Lease_upCertifiPhoto"
	},
	EVENTS : {
		"click" : {
			"#saveBtn" : "submit"
		}
//		"focus" : {
//			"#id_card_input" : "onIDcardFocus"
//		},
//		"blur" : {
//			"#id_card_input" : "onIDcardBlur"
//		}
	},
	init : function(){
		this.statics.ltie9 = $("#memCertifyContainer").hasClass("lt-ie9") ? true : false;
	},
	submit : function(that,e){
		e.preventDefault();
        var allow = true;
		var url = that.statics.api;
		var formLoading = $("#formLoading");
		var com_type = $("#hidInp_com_type").val();
		var did = $("#hidInp_did").val();
		var data = {};
		data["com_type"] = com_type;
		data["did"] = did;
		//身份证
		//var id_card = $("#id_card_input");
		//手持身份证正面
		var id_card_hand = $("#id_card_hand_hid_inp");
		//手持身份证背面
		var id_card_B = $("#id_card_B_hid_inp");
		//其它证件
		var other_les = $("#other_les_hid_inp");
		//营业执照
		var bsn_les_hid_inp = $("#bsn_les_hid_inp");
		//营业许可证
		var opt_les_hid_inp = $("#opt_les_hid_inp");
		//税务登记证
		var tax_les_hid_inp = $("#tax_les_hid_inp");
		//组织机构代码证
		var org_les_hid_inp = $("#org_les_hid_inp");
		//资源方授权书
		var auth_les_hid_inp = $("#auth_les_hid_inp");

		if(id_card.length) data["id_card"] = id_card.val();
		if(id_card_hand.length) data["id_card_hand"] = id_card_hand.val();
		if(id_card_B.length) data["id_card_B"] = id_card_B.val();
		if(other_les.length) data["other_les"] = other_les.val();
		if(bsn_les_hid_inp.length) data["bsn_les"] = bsn_les_hid_inp.val();
		if(opt_les_hid_inp.length) data["opt_les"] = opt_les_hid_inp.val();
		if(tax_les_hid_inp.length) data["tax_les"] = tax_les_hid_inp.val();
		if(org_les_hid_inp.length) data["org_les"] = org_les_hid_inp.val();
		if(auth_les_hid_inp.length) data["auth_les"] = auth_les_hid_inp.val();


        //检查必须上传的证件是否都上传完整了
        that.container.find(".hidInp_imgurl").each(function(){
            var hidInp = $(this);
            var require = hidInp.attr("data-require");
            var msg = hidInp.attr("data-msg") || "请上传必须上传的证件";
            var val = hidInp.val();
            if(require && !val){
                allow = false;
                alert(msg);
                return false;
            }
        })

		//if(!id_card_hand){
		//	alert("请上传手持身份证照正面");
		//	return false;
		//}
        if(!allow) return false;
		PFT_GLOBAL.G.Ajax({
			url : "/r/Member_MemberCertifi/saveCertifiInfo",
			type : "post",
			data : data,
			loading : function(){
				formLoading.show();
				if(that.statics.ltie9) formLoading.css("opacity",0.8);
			},
			removeLoading : function(){
				formLoading.hide();
			},
			timeout : function(){
				console && console.log("保存质资认证超时");
			},
			serverError : function(){
				console && console.log("保存质资认证服务在出错");
			}
		},function(res){
			if(res.code=="200"){
				PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">保存成功</p>');
			}else{
				PFT_GLOBAL.U.Alert("fail",'<p style="width:160px;">'+res.msg+'</p>')
			}
		})
	},
	onIDcardFocus : function(that,e){
		$("#idcardErrorTip").hide();
	},
	onIDcardBlur : function(that,e){
		var tarInp = $("#id_card_input");
		if(!tarInp.val()) $("#idcardErrorTip").show();
	}
})*/
//身份证正面
var uploadorname_1 = $("#imgLoaderContainer_1").find("input[name=callback]").val();
if(uploadorname_1){

	uploadorname_1 = uploadorname_1.split(".")[0];
	window[uploadorname_1] = new ImgUploador({container:$("#imgLoaderContainer_1")});
}

//身份证背面
var uploadorname_2 = $("#imgLoaderContainer_2").find("input[name=callback]").val();
if(uploadorname_2){
	uploadorname_2 = uploadorname_2.split(".")[0];
	window[uploadorname_2] = new ImgUploador({container:$("#imgLoaderContainer_2")});
}

/*//其它证件
var uploadorname_3 = $("#imgLoaderContainer_3").find("input[name=callback]").val();
if(uploadorname_3){
	uploadorname_3 = uploadorname_3.split(".")[0];
	window[uploadorname_3] = new ImgUploador({container:$("#imgLoaderContainer_3")});
}

//营业执照
var uploadorname_bsn_les = $("#imgLoaderContainer_bsn_les").find("input[name=callback]").val();
if(uploadorname_bsn_les){
	uploadorname_bsn_les = uploadorname_bsn_les.split(".")[0];
	window[uploadorname_bsn_les] = new ImgUploador({container:$("#imgLoaderContainer_bsn_les")});
}

//营业许可证
var uploadorname_opt_les = $("#imgLoaderContainer_opt_les").find("input[name=callback]").val();
if(uploadorname_opt_les){
	uploadorname_opt_les = uploadorname_opt_les.split(".")[0];
	window[uploadorname_opt_les] = new ImgUploador({container:$("#imgLoaderContainer_opt_les")});
}

//税务登记证
var uploadorname_tax_les = $("#imgLoaderContainer_tax_les").find("input[name=callback]").val();
if(uploadorname_tax_les){
	uploadorname_tax_les = uploadorname_tax_les.split(".")[0];
	window[uploadorname_tax_les] = new ImgUploador({container:$("#imgLoaderContainer_tax_les")});
}

//组织机构代码证
var uploadorname_org_les = $("#imgLoaderContainer_org_les").find("input[name=callback]").val();
if(uploadorname_org_les){
	uploadorname_org_les = uploadorname_org_les.split(".")[0];
	window[uploadorname_org_les] = new ImgUploador({container:$("#imgLoaderContainer_org_les")});
}

//资源方授权书
var uploadorname_auth_les = $("#imgLoaderContainer_auth_les").find("input[name=callback]").val();
if(uploadorname_auth_les){
	uploadorname_auth_les = uploadorname_auth_les.split(".")[0];
	window[uploadorname_auth_les] = new ImgUploador({container:$("#imgLoaderContainer_auth_les")});
}
*/






//new FormSubmit({container:$("#memCertifyContainer")})






